// @Software: serve
// @File : usr_icon
// @Author : vince
// @Time : 2021/9/23 17:12
// @Description  请填写文件描述
package v1

import (
	. "gintify/core"
	"gintify/model"
	"gintify/response"
	"github.com/gin-gonic/gin"
	"strconv"
)

type IIconController interface {
	IRestController
}

type IconController struct {
	// 空结构体
}

func NewIconController() *IconController {
	DB.Debug().AutoMigrate(&model.UsrIcon{})
	return &IconController{}
}

// @Summary 新增图标
// @Description 在数据库中新增图标
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Success 200 {object} model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons [post]
func (r IconController) Post(c *gin.Context) {
	var icon = model.UsrIcon{}
	if err := c.ShouldBindJSON(&icon); err != nil {
		response.Error(c, "绑定错误", err)
		return
	}
	if icon.Name == "" {
		response.Error(c, "绑定错误", nil)
		return
	}
	if err := DB.Debug().Create(&icon).Error; err != nil {
		response.Error(c, "创建图标记录失败", err)
		return
	}
	response.Success(c, "创建图标记录成功", icon)
}

// @Summary 获取图标列表
// @Description 在数据库中获取图标列表信息
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Success 200 {object} []model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons [get]
func (r IconController) GetList(c *gin.Context) {
	var icons []model.UsrIcon
	var page = c.DefaultQuery("page", "100")
	var size = c.DefaultQuery("size", "1")
	var search = c.Query("search")
	var total int64
	var db = DB
	limit := TransStringToInt(size)
	offset := limit * (TransStringToInt(page) - 1)
	if search != "" {
		db = db.Where("name LIKE ?", "%" + search + "%")
	}
	db.Model(&model.UsrIcon{}).Count(&total);
	if err := db.Debug().Limit(limit).Offset(offset).Find(&icons).Error; err != nil {
		response.Error(c, "查询list失败", err)
		return
	}
	response.Success(c, "获取图标列表成功",
		gin.H{
			"total": total,
			"list":  icons,
		})
}

// @Summary 获取图标
// @Description 在数据库中获取图标信息
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "icon id in database"
// @Success 200 {object} model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons/{id} [get]
func (r IconController) Get(c *gin.Context) {
	var icon model.UsrIcon
	id := c.Param("id")
	//idx, err := strconv.Atoi(ids)
	//if err != nil || idx < 1 {
	//	response.Fail(c, "URL路径有错误", err)
	//	return
	//}

	if err := DB.Debug().Where("id = ?", id).First(&icon).Error; err != nil {
		response.Error(c, "查询失败", nil)
		return
	}
	//var menus = []model.UsrMenu{}
	//if err:=DB.Model(&icon).Related(&menus, "Menus").Error;err!=nil{
	//	response.Error(c, "查询失败", nil)
	//	return
	//}
	//icon.Menus = menus
	//fmt.Printf("%v\n", menus)
	//if err := DB.Model(&icon).Association("Menus").Find(&icon.Menus).Error; err != nil{
	//	fmt.Println("关联失败")
	//}
	response.Success(c, "获取图标信息成功", icon)
}

// @Summary 更新图标
// @Description 在数据库中更新图标信息
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "icon id in database"
// @Success 200 {object} model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons/{id} [put]
func (r IconController) Put(c *gin.Context) {
	ids := c.Param("id")
	idx, err := strconv.Atoi(ids)
	if err != nil || idx < 1 {
		response.Fail(c, "URL路径有错误", err)
		return
	}
	var icon = model.UsrIcon{}
	if err := c.ShouldBindJSON(&icon); err != nil {
		response.Error(c, "绑定错误", err)
		return
	}
	if err := DB.Model(&model.UsrIcon{}).Where("id = ?", idx).Updates(&icon).Error; err != nil {
		response.Error(c, "更新图标失败", err)
		return
	}
	response.Updated(c, "更新图标成功", nil)
}

// @Summary 更新图标
// @Description 在数据库中更新图标信息
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "icon id in database"
// @Success 200 {object} model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons/{id} [patch]
func (r IconController) Patch(c *gin.Context) {
	panic("implement me")
}

// @Summary 删除图标
// @Description 在数据库中软删除图标
// @Tags 图标
// @Accept  json
// @Produce  json
// @Security ApiKeyAuth
// @Param id path uint64 true "icon id in database"
// @Success 200 {object} model.UsrIcon "{"code":0,"msg":"OK","data": {}}"
// @Router /api/v1/icons/{id} [delete]
func (r IconController) Delete(c *gin.Context) {
	id := c.Param("id")

	if err := DB.Debug().Where("id = ?", id).Delete(&model.UsrIcon{}).Error; err != nil {
		response.Error(c, "删除图标失败", err)
		return
	}
	response.Deleted(c, "删除图标成功", nil)
}
